package com.woniu.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.Service.RoleService;
import com.woniu.annotation.CheckPermission;
import com.woniu.entity.Role;
import com.woniu.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author: Gwj
 * @Date: 2024/7/3 19:39
 */
@RestController
@Slf4j
@RequestMapping("/role")
@Api(value = "角色功能接口", tags = "所有的角色功能")
public class RoleController {
    @Resource
    private RoleService roleService;

    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @GetMapping("/findRoleInfoByPage")
    @CheckPermission("role:select")
    @ApiOperation(value = "角色分页查询接口")
    public JsonResult<PageInfo<Role>> findRoleInfoByPage(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize) throws Exception {
        PageInfo<Role> roleInfoByPage = roleService.findRoleInfoByPage(pageNum, pageSize);
        return JsonResult.build(200, "success", roleInfoByPage);
    }

    /**
     * 新增角色
     *
     * @param role
     * @return
     * @throws Exception
     */
    @PostMapping("/addRole")
    @CheckPermission("role:insert")
    @ApiOperation(value = "添加角色接口")
    public JsonResult<Integer> addRole(Role role) throws Exception {
        int insert = roleService.addRole(role);
        if (insert > 0) {
            return JsonResult.build(200, "添加成功", insert);
        } else {
            return JsonResult.build(500, "添加失败", insert);
        }
    }

    /**
     * 修改角色
     *
     * @param role
     * @return
     * @throws Exception
     */
    @PutMapping("/updateRole")
    @CheckPermission("role:update")
    @ApiOperation(value = "修改角色接口")
    public JsonResult<Integer> updateRole(Role role) throws Exception {
        int update = roleService.updateRoleInfoById(role);
        if (update > 0) {
            return JsonResult.build(200, "修改成功", update);
        } else {
            return JsonResult.build(500, "修改失败", update);
        }
    }

    /**
     * 删除角色 逻辑删除
     *
     * @param role
     * @return
     * @throws Exception
     */
    @PutMapping("/deleteRole")
    @CheckPermission("role:delete")
    @ApiOperation(value = "删除角色接口")
    public JsonResult<Integer> deleteRole(Role role) throws Exception {
        int delete = roleService.changeRoleIsDeleted(role);
        if (delete > 0) {
            return JsonResult.build(200, "删除成功", delete);
        } else {
            return JsonResult.build(500, "删除失败", delete);
        }
    }

    /**
     * 新增时的重名验证
     *
     * @param roleName
     * @return
     * @throws Exception
     */
    @GetMapping("/checkRoleName/{roleName}")
    @CheckPermission("role:select")
    @ApiOperation(value = "检查角色名称是否存在")
    public JsonResult<Boolean> checkRoleName(@PathVariable String roleName) throws Exception {
        Role role = roleService.checkRoleName(roleName);
        if (role != null) {
            if (role.getRoleName().equals(roleName)) {
                return JsonResult.build(500, "此角色名称已存在，不可使用", false);
            } else {
                return JsonResult.build(200, "此角色名称可以使用", true);
            }
        } else {
            return JsonResult.build(200, "此角色名称可以使用", true);
        }
    }

    /**
     * 修改时的重名验证
     *
     * @param roleId
     * @param roleName
     * @return
     * @throws Exception
     */
    @GetMapping("/checkRoleByNameAndId/{roleId}/{roleName}")
    @CheckPermission("role:select")
    @ApiOperation(value = "检查角色名称是否存在，排除当前的")
    public JsonResult<Boolean> checkRoleByNameAndId(@PathVariable Integer roleId, @PathVariable String roleName) throws Exception {
        Role role = roleService.selectRoleByNameAndId(roleName, roleId);
        if (role == null) {
            return JsonResult.build(200, "此角色名称可以使用", true);
        } else {
            return JsonResult.build(500, "此角色名称已存在，不可使用", false);
        }
    }

    /**
     * 模糊查询 并且分页
     *
     * @param pageNum
     * @param pageSize
     * @param roleName
     * @return
     * @throws Exception
     */
    @GetMapping("/findRoleInfoLikeName")
    @CheckPermission("role:select")
    @ApiOperation(value = "模糊查询角色")
    public JsonResult<PageInfo<Role>> findRoleInfoLikeName(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize, String roleName) throws Exception {
        PageInfo<Role> roleInfoByPage = roleService.selectRoleInfoByRoleName(roleName, pageNum, pageSize);
        return JsonResult.build(200, "success", roleInfoByPage);
    }
}
